package org.x.user.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.x.api.system.dto.TenantDTO;
import org.x.api.system.feign.RemoteTenantService;
import org.x.common.base.constants.TenantStatus;
import org.x.user.entity.SysTenants;
import org.x.user.mapper.SysTenantsMapper;

import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "1.0.0", group = "x-tenant")
public class RemoteTenantServiceImpl implements RemoteTenantService {

    private SysTenantsMapper tenantMapper;

    @Autowired
    public void setTenantMapper(SysTenantsMapper tenantMapper) {
        this.tenantMapper = tenantMapper;
    }

    @Override
    public boolean isValidTenant(String tenantId) {
        return tenantMapper.exists(
                new QueryWrapper<SysTenants>()
                        .eq("tenant_id", tenantId)
                        .eq("status", TenantStatus.ACTIVE) // 状态1表示有效租户
        );
    }

    @Override
    public List<TenantDTO> getActiveTenants() {
        return tenantMapper.selectList(
                new QueryWrapper<SysTenants>()
                        .eq("tenant_status", TenantStatus.ACTIVE)
                        .orderByAsc("create_time")
        ).stream().map(t ->
                new TenantDTO(t.getId(), t.getTenantName(),t.getTenantCode(), t.getTenantStatus())
        ).collect(Collectors.toList());
    }
}
